package org.myblog.myblogserver.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.myblog.myblogserver.config.interceptor.JwtTokenInterceptor;
import org.myblog.myblogserver.config.interceptor.ZhiGeTokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration
@RequiredArgsConstructor
@Slf4j
public class SwaggerConfig extends WebMvcConfigurationSupport {
    private final JwtTokenInterceptor jwtTokenInterceptor;
    private final ZhiGeTokenInterceptor zhiGeTokenInterceptor;


    /**
     * 注册自定义拦截器
     *
     * @param registry（用于注册自定义拦截器）
     * return 没有返回值
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/user/**")  //会拦截的接口
                .addPathPatterns("/admin/**")
                .addPathPatterns("/article/**")
                .addPathPatterns("/message/**")
                .addPathPatterns("/official/**")
                .addPathPatterns("/tabs/**")

                .excludePathPatterns("/user/login")  // 不会拦截的接口
                .excludePathPatterns("/user/sendcode")
                .excludePathPatterns("/user/sign")
                .excludePathPatterns("/user/selectCount")

                .excludePathPatterns("/article/selecteq")  // 文章查询
                .excludePathPatterns("/article/addViews")  // 文章查询
                .excludePathPatterns("/message/selecteq")  // 消息查询
                .excludePathPatterns("/message/selectCount")  // 消息总数查询
                .excludePathPatterns("/official/selectall")  //文案查询
                .excludePathPatterns("/tabs/selecteq");  // 标签查询

        registry.addInterceptor(zhiGeTokenInterceptor)
                .addPathPatterns("/user/selecteq")
                .addPathPatterns("/user/update")
                .addPathPatterns("/user/deleteAlleq")
                .addPathPatterns("/user/selectpage")

                .addPathPatterns("/admin/sign")
                .addPathPatterns("/admin/deleteAll")

                .addPathPatterns("/article/insert")
                .addPathPatterns("/article/update")
                .addPathPatterns("/article/deleteAlleq")

                .addPathPatterns("/message/deleteAlleq")

                .addPathPatterns("/official/insert")
                .addPathPatterns("/official/deleteAll")

                .addPathPatterns("/tabs/insert")
                .addPathPatterns("/tabs/deleteAlleq");

    }



    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket(){
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("个人博客")
                .version("2.0")
                .description("个人博客相关接口")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.myblog.myblogserver.code.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置swagger静态资源映射
     * 访问地址：http://localhost:端口号/doc.html，可以看到接口文档
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");
    }
}
